//
//  TWBaseViewController.m
//  TWHomework-WeChat
//
//  Created by wzb on 2019/11/12.
//  Copyright © 2019 WZB. All rights reserved.
//

#import "TWBaseViewController.h"
#import "TWBaseViewModel.h"
#import <MJRefresh.h>

@interface TWBaseViewController ()

@property (nonatomic, strong) NSMutableArray *viewModelArray;

@end

@implementation TWBaseViewController

#pragma mark - lazy
- (NSMutableArray *)viewModelArray {
    if (!_viewModelArray) {
        _viewModelArray = [NSMutableArray array];
    }
    return _viewModelArray;
}

#pragma mark - public
/// 绑定viewModel
- (TWBaseViewModel *)registerVM:(TWBaseViewModel *)viewModel {
    [self.viewModelArray addObject:viewModel];
    return viewModel;
}

/// 添加下拉刷新控件
- (void)addPullRefresh:(UIScrollView *)scrollView refreshingAction:(SEL)action
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:action];
    header.lastUpdatedTimeLabel.textColor = [UIColor grayColor];
    header.stateLabel.textColor = [UIColor grayColor];
    header.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    header.stateLabel.font = [UIFont systemFontOfSize:12];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
    header.lastUpdatedTimeKey = NSStringFromClass(self.class);
    header.lastUpdatedTimeLabel.hidden = YES;
    scrollView.mj_header = header;
}

/// 添加上拉加载更多控件
- (void)addPullLoadMore:(UIScrollView *)scrollView loadMoreAction:(SEL)action
{
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:action];
    footer.stateLabel.textColor = [UIColor grayColor];
    footer.stateLabel.font = [UIFont systemFontOfSize:12];
    footer.refreshingTitleHidden = YES;
    scrollView.mj_footer = footer;
}

/// 显示没有更多数据
- (void)showNoMoreData:(UIScrollView *)scrollView
{
    [scrollView.mj_footer endRefreshingWithNoMoreData];
}

/// 重置没有更多的数据（消除没有更多数据的状态）
- (void)resetNoMoreData:(UIScrollView *)scrollView
{
    [scrollView.mj_footer resetNoMoreData];
}

/// 隐藏显示没有更多数据
- (void)hideNoMoreData:(UIScrollView *)scrollView
{
    [scrollView.mj_footer resetNoMoreData];
    scrollView.mj_footer.hidden = NO;
}

/// 结束刷新
- (void)endRefresh:(UIScrollView *)scrollView
{
    [scrollView.mj_header endRefreshing];
    [scrollView.mj_footer endRefreshing];
}

/// 可以在子类中重写此方法用来绑定viewModel
- (void)initViewModelBinding { }
/// UI创建
- (void)initUIView { }
/// UI布局
- (void)layoutUI { }

/// 页面销毁时取消所有该页的网络请求 ，
/// 通常只需写入： [_viewModel cancelAllHTTPRequest];即可
- (void)deallocHTTPRequest {
    for (TWBaseViewModel *viewModel in self.viewModelArray) {
        [viewModel cancelAllHTTPRequest];
    }
}

#pragma mark - systemMethod
- (void)viewDidLoad {
    [super viewDidLoad];
    
    /// 基本设置
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.backBarButtonItem = nil;
    
    /// 调用内置方法
    /// 一般子类来实现
    [self initViewModelBinding];
    [self initUIView];
    [self layoutUI];
}

- (void)dealloc {
    /// 销毁HTTP请求
    /// 移除通知监听
    /// 打印一下当前销毁的控制器
    [self deallocHTTPRequest];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}



@end
